package com.smmisia.auth.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.alicp.jetcache.anno.CacheInvalidate;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.Cached;
import com.google.zxing.WriterException;
import com.smmisia.common.request.auth.UserRequest;
import com.smmisia.common.request.auth.UserUpdateRequest;
import com.smmisia.auth.service.UserService;
import com.smmisia.auth.service.impl.QRCodeGenerationService;
import com.smmisia.common.Result.Result;
import com.smmisia.common.response.auth.UserResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.cache.annotation.CacheRemove;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * @author: smmisia
 * @date: 2024-09-08
 * @description: 用户信息管理
 */
@RestController
@RequestMapping("/userInfo")
@Slf4j
@Tag(name = "用户管理")
public class UserInfoController {

    @Autowired
    private UserService userService;

    @Autowired
    private QRCodeGenerationService qrCodeGenerationService;

    /**
     * 获取当前用户信息
     *
     * @return
     */
    @GetMapping("/info")
    @Operation(summary = "获取用户信息", description = "获取用户信息")
    public Result<UserResponse> info() {
        return userService.info();
    }

    /**
     * 后台管理系统 获取用户列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    @Operation(summary = "获取用户列表", description = "获取用户列表")
    public Result<List<UserResponse>> infoList(@RequestParam(defaultValue = "1") Integer pageNum,
                                               @RequestParam(defaultValue = "10") Integer pageSize) {
        return userService.infoList(pageNum, pageSize);
    }

    /**
     * 启用或禁用用户
     *
     * @param id
     * @return
     */
    @GetMapping("/status")
    @Operation(summary = "启用或禁用用户", description = "启用或禁用用户")
    public Result<String> status(@RequestParam @NotNull(message = "id不能为空") Integer id) {
        return userService.status(id);
    }

    /**
     * 修改用户信息
     *
     * @param userRequest
     * @return
     */
    @PostMapping("/update")
    @Operation(summary = "修改用户信息", description = "修改用户信息")
    public Result<String> update(@Valid @RequestBody UserRequest userRequest) {
        return userService.updateUserInfo(userRequest);
    }

    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    @GetMapping("/delete")
    @Operation(summary = "删除用户", description = "删除用户")
    public Result<String> delete(@RequestParam @NotNull(message = "id不能为空") Integer id) {
        return userService.deleteUserInfo(id);
    }

    /**
     * 测试接口 判断用户是否登录
     *
     * @return
     */
    @GetMapping("/isLogin")
    @Operation(summary = "判断用户是否登录", description = "判断用户是否登录")
    public String isLogin() {
//        String loginId = LoginUtil.getLoginId();
//        log.info("UserController.login.id:{}", loginId);
//        return "当前会话是否登录：" + StpUtil.isLogin();
        boolean login = StpUtil.isLogin();
        String loginId = (String) StpUtil.getLoginId();
        if (login) {
            return "用户：" + loginId + "当前会话已经登录";
        }
        return "当前会话未登录";
    }

    /**
     * 用户条件查询
     *
     * @param email
     * @param
     * @return
     */
    @GetMapping("/search")
    @Operation(summary = "用户条件查询", description = "用户条件查询")
    public Result<String> search(@RequestParam(name = "email")  String email) {
        return userService.search(email);
    }

    /**
     * 修改用户信息(客户端)
     *
     * @param userDTO
     * @return
     */
    @PostMapping("/updateUserInfo")
    @Operation(summary = "修改用户信息(客户端)", description = "修改用户信息(客户端)")
    public Result<String> updateUserInfo(@Valid @RequestBody UserUpdateRequest userDTO) {
        return userService.updateUserInfos(userDTO);
    }

    /**
     * 获取我的二维码名片
     */
    @GetMapping("/qrcode")
    @Operation(summary = "获取我的二维码名片", description = "获取我的二维码名片")
    public Result<String> qrcode(@RequestParam @NotBlank(message = "内容不能为空") String content) throws IOException, WriterException {
        String qrCodeUrl = qrCodeGenerationService.saveQRCodeImageToTempFile(content);
        HashMap<String, String> map = new HashMap<>();
        map.put("qrCodeUrl", qrCodeUrl);
        return Result.ok(map);
    }

    /**
     * 用户退出登录
     *
     * @return
     */
    @GetMapping("/logout")
    @Operation(summary = "用户退出登录", description = "用户退出登录")
    public Result<String> logout() {
        StpUtil.logout();
        return Result.ok("退出成功");
    }
}